package com.kzd.gtboon.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtboon.base.BaseViewModel
import com.kzd.gtboon.entity.*
import com.kzd.gtboon.jetpack.api.*
import com.module.lemlin.http.*
import com.module.lemlin.owner.setRxLifeScope
import java.io.Serializable

class GameViewModel(application: Application) : BaseViewModel(application) {

    data class GameUpdate(
        val type: String,
        val isLoadMore: Boolean = false,
    ) : Serializable

    private var page: Int = 1
    private var limit: Int = 10

    lateinit var mGameId: String

    val selectedClassifyIdLiveData = MutableLiveData<GameUpdate>()

    val gameDetailsLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<GameDetails>>>()
    fun gameDetails(gid: String) {
        mGameId = gid
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataT<GameDetails>>(gameDetailsLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<GameDetails>) {
                super.onSuccess(resp.apply { mGameId = data.id ?: return@apply })
                gameGuess()
            }
        }) {
            appGameInfo(gid)
        }
    }

    val gameGuessLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBean>>>()
    fun gameGuess() {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<GameBean>>(gameGuessLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameBean>) {
                super.onSuccess(resp)
                this@GameViewModel.page += 1
            }
        }) {
            newGetDetailList(page.toString(), mGameId)
        }
    }

    val gameWelfareLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<GameWelfare>>>()
    fun gameWelfare() {
        setRxLifeScope(SubscribeViewModel(gameWelfareLiveData)) {
            newWelfare(mGameId)
        }
    }

    val gameServiceLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameService1>>>()
    fun gameService(gid: String = mGameId, isLoadMore: Boolean = false, limit: Int = this.limit) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<GameService1>>(gameServiceLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameService1>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            newGameServer(gid, page.toString(), limit.toString())
        }
    }

    val serviceRemindLiveData = MutableLiveData<HttpResponseBody<ServiceRemindResponse>>()
    fun gameServiceRemind(id: String, isRemind: Int, position: Int) {
        setRxLifeScope(object : SubscribeViewModel<ServiceRemindResponse>(serviceRemindLiveData) {
            override fun onSuccess(resp: ServiceRemindResponse) {
                super.onSuccess(resp.apply {
                    this.isRemind = if (isRemind == 0) 1 else 0
                    this.position = position
                })
            }
        }) {
            newRemindServer(id)
        }
    }

    val gameClassifyLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<ClassifyTab>>>()
    fun gameClassify() {
        setRxLifeScope(SubscribeViewModel(gameClassifyLiveData)) {
            appClassify()
        }
    }

    val gameListLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBean>>>()
    fun gameList(type: String, isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<GameBean>>(gameListLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameBean>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            appClassifyGame(type, page.toString())
        }
    }

    val gameFeaturedLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<GameFeatured>>>()
    fun gameFeatured(systemType: String = "1", ios: String = "0") {
        setRxLifeScope(SubscribeViewModel(gameFeaturedLiveData)) {
            newGetSelectedList(systemType, ios)
        }
    }

    val gameRankingLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBean>>>()
    fun gameRanking(type: String, cid: String = "0", isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<GameBean>>(gameRankingLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameBean>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            appRanking(type, cid, page.toString())
        }
    }

    val gameExchangeCodeLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<String>>>()
    fun gameExchangeCode(id: String) {
        setRxLifeScope(SubscribeViewModel(gameExchangeCodeLiveData)) {
            personalGetExchangeCode(id)
        }
    }

}